import { extendApi } from '@inbiz/utils';
import { MutableRefObject, Dispatch, SetStateAction } from 'react';
import { IModalRef } from '@inbiz/render';
import { IOperation } from '.';

export default extendApi(
  (
    _events: any,
    {
      $disabled,
      updateOperation,
      $trigger,
      modalRef,
      triggerClick,
    }: {
      modalRef: MutableRefObject<IModalRef>;
      $disabled: Dispatch<SetStateAction<boolean>>;
      $trigger: Dispatch<SetStateAction<string[]>>;
      updateOperation: (flag: string, options: object) => void;
      triggerClick: (flag: string) => void;
    },
  ) => {
    return {
      setDisabled: (disabled: boolean) => {
        // 设置只读状态
        $disabled(disabled);
      },
      setButtonDisabled(flag: string, disabled: boolean) {
        updateOperation(flag, { disabled });
      },
      setButtonDanger(flag: string, danger: boolean) {
        updateOperation(flag, { danger });
      },
      setButtonOption(flag: string, option: object) {
        if (option && typeof option === 'object') {
          updateOperation(flag, option);
        }
      },
      setTrigger(triggers: ('click' | 'hover' | 'contextMenu')[]) {
        if (Array.isArray(triggers)) {
          $trigger(triggers);
        }
      },
      close: () => {
        // 关闭弹窗
        modalRef.current?.onClose();
      },
      getPage: () => {
        // 获取弹框的inbiz
        return modalRef.current?.getPage();
      },
      submit: () => {
        // 触发表单弹框操作
        modalRef.current?.submit();
      },
      triggerClick(flag: string) {
        //触发指定按钮弹框事件
        triggerClick(flag);
      },
    };
  },
);
